<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="panel panel-default operation_case" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form " action="" lay-filter="deptInfo" style="width: 700px;margin-top: 10px">
            <input name="id" hidden/>
            <div class="layui-form-item">
                <label class="layui-form-label">行业名称</label>
                <div class="layui-input-block">
                    <input type="name" name="name" placeholder="请输入行业名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <input type="bz" name="bz" placeholder="请输入备注" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="status" lay-skin="switch" lay-filter="switch" lay-text="有效|无效" checked>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
                    <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="case_table_div">
    <table class="layui-hide" id="case_table" lay-filter="case_table"></table>
</div>
</body>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addNewCase" >添加</button>
    </div>
</script>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" >修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" >删除</a>
</script>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    //获取token
    let token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    let tokenQuery = token.replace("#", "%23");
    let tableIns1;
    let tableAjxx;
    let table = layui.table;
    let form = layui.form;
    let layer = layui.layer;
    let upload = layui.upload;
    let $ = jQuery = layui.jquery;
    let laydate = layui.laydate;
    let tree = layui.tree;
    let startTime = null;
    let endTime = null;
    layui.config({
        base: '/'
    }).extend({
        eleTree: 'layui-ext/eleTree/eleTree'
    }).use(['table', 'laypage', 'layer', 'laydate', 'tree', 'eleTree','upload'], function () {
        let eleTree = layui.eleTree;
        tableIns1 = table.render({
            elem: '#case_table'
            , contentType: 'application/json'
            , headers: {"authorization": token}
            , page: true //开启分页
            , url: '/attention/zbxazdhyzd/list' //数据接口
            , method: 'POST'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
                }
            }
            , cols: [
                [
                    {field: 'name', title: '重点行业名称', width: 250},
                    {
                        field: 'status', title: '状态', width: 100, templet: function (item) {
                            if (item.status === 1) {
                                return '  <input type="checkbox" lay-skin="switch" lay-text="有效|无效" checked disabled>';
                            }
                            if (item.status === 0) {
                                return '  <input type="checkbox" lay-skin="switch" lay-text="有效|无效" disabled>';
                            }
                        }
                    },
                    {field: 'bz', title: '备注', width: 400},
                    {title: '操作', width: 250, toolbar: '#tool'}
                ]
            ]
            , toolbar: '#toolbar'
        });

        laydate.render({
            elem: '#createTime'
            , type: 'datetime'
            , range: '~'
            , done: function (value, date, endDate) {
                if (value != null && value != undefined && value != "") {
                    startTime = value.split("~")[0];
                    endTime = value.split("~")[1];
                } else {
                    startTime = null;
                    endTime = null;
                }
            }
        });
        table.on('toolbar(case_table)', function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case 'addNewCase':
                    //隐藏表格
                    $(".case_table_div").hide();
                    //显示编辑窗口
                    $(".operation_case").show();
                    $(".title").html("新增专项案件");
                    $(".operation_case input[name=id]").val("");
                    $(".operation_case input[name=name]").val("");
                    $(".operation_case input[name=status]").attr('checked', 'checked');
                    $(".operation_case input[name=status]").attr('type', 'hidden').val(1);
                    let x = document.getElementsByClassName("layui-unselect layui-form-switch");
                    x[0].setAttribute("class", "layui-unselect layui-form-switch layui-form-onswitch");
                    let d = document.getElementsByTagName('em')[0];
                    d.firstChild.nodeValue = '有效';
                    form.render(); //更新全部
                    break;
            }
        });
        table.on('tool(case_table)', function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case 'del':
                    tipDialog(data.id);
                    break;
                case 'edit':
                    $(".case_table_div").hide();
                    //显示编辑窗口
                    $(".operation_case").show();
                    $(".title").html("编辑行业");
                    getCaseDetail(data.id);
                    break;
            }
        });
        let getCaseDetail = function (id) {
            CoreUtil.sendGet("/attention/zbxazdhyzd/detail/" + id, null, function (res) {
                $(".operation_case input[name=id]").val(res.data.id);
                $(".operation_case input[name=name]").val(res.data.name);
                $(".operation_case input[name=bz]").val(res.data.bz);
                if (res.data.status == 1) {
                    $(".operation_case input[name=status]").attr('checked', 'checked');
                    $(".operation_case input[name=status]").attr('type', 'hidden').val(1);
                    let x = document.getElementsByClassName("layui-unselect layui-form-switch");
                    x[0].setAttribute("class", "layui-unselect layui-form-switch layui-form-onswitch");
                    let d = document.getElementsByTagName('em')[0];
                    d.firstChild.nodeValue = '有效';
                } else {
                    $(".operation_case input[name=status]").attr('type', 'hidden').removeAttr("checked").val(0);
                    let x = document.getElementsByClassName("layui-unselect layui-form-switch");
                    x[0].setAttribute("class", "layui-unselect layui-form-switch");
                    let d = document.getElementsByTagName('em')[0];
                    d.firstChild.nodeValue = '失效';
                }
                $(".operation_case_dataScope input[name=roleId]").val(res.data.id);
                if (!CoreUtil.isEmpty(res.data.dataScope)) {
                    $("#dataScopeS").find('option[value="' + res.data.dataScope + '"]').prop('selected', true);
                    $("#dataScope").val(res.data.dataScope)
                    if (res.data.dataScope == "2") {
                        $("#choose-dept").show();
                    }
                } else {
                    $("#dataScopeS option:first").prop("selected", 'selected');
                }
                form.render(); //更新全部

            });
        }

        form.on('switch(switch)', function () {
            $(".operation_case input[name=status]").attr('type', 'hidden').val(this.checked ? 1 : 0);

        });
        $("#btn_cancel").click(function () {
            $(".case_table_div").show();
            $(".operation_case").hide();
            $(".operation_case_dataScope").hide();
            return false;
        });
        $("#btn_cancel_imp").click(function () {
            $(".case_table_div").show();
            $(".operation_case").hide();
            $(".operation_case_dataScope").hide();
            return false;
        });
        form.on('select(select-datascope)', function (data) {
            $("#dataScope").val(data.value)
            if (data.value == "2") {
                $("#choose-dept").show();
            } else {
                $("#choose-dept").hide();
            }
        });
        //监听提交 新增案件
        form.on('submit(submit)', function (data) {
            console.log(data);
            let params = {
                id: data.field.id,
                name: data.field.name,
                status: data.field.status,
                bz:data.field.bz
            }
            if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
                CoreUtil.sendPost("/attention/zbxazdhyzd/add", params, function (res) {
                    $(".case_table_div").show();
                    $(".operation_case").hide();
                    $(".operation_case_dataScope").hide();
                    search();
                });
            } else {
                CoreUtil.sendPost("/attention/zbxazdhyzd/update", params, function (res) {
                    //显示表格
                    $(".case_table_div").show();
                    //隐藏编辑窗口
                    $(".operation_case").hide();
                    $(".operation_case_dataScope").hide();
                    search();
                });
            }
            return false;
        });
        let tipDialog = function (id) {
            layer.open({
                content: '确定要删除么',
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendPost("/attention/zbxazdhyzd/deleted/" + id, null, function (res) {
                        layer.msg(res.msg);
                        search();
                    });
                }
            });
        }
    });
    //执行查询
    function search() {
        tableIns1.reload({
            where: { //设定异步数据接口的额外参数，任意设
                status: $("#status").val(),
                startTime: startTime,
                endTime: endTime,
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
</script>
</html>